package org.example;

import cn.hutool.core.util.StrUtil;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.example.execute.Controller;
import org.example.execute.Service;
import org.example.execute.ServiceImpl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

@Mojo(name = "generate")
public class Generate extends AbstractMojo {

    @Parameter(name = "configurationFile")
    private File configurationFile;

    public void execute() {
        try {
            Properties properties = new Properties();
            properties.load(new FileInputStream(configurationFile.getPath()));
            getLog().info("file " + properties.getProperty("generator.list"));
            String[] entitys = properties.getProperty("generator.list").split(",");
            for (String entity : entitys) {
                Boolean service = Boolean.parseBoolean(properties.getProperty("generator.service"));
                if (service) {
                    String path = properties.getProperty("generator.service.path");
                    getLog().info(path);
                    getLog().info(entity + "service 生成");
                    Service.execute(getLog(), path, entity);
                }

                Boolean serviceImpl = Boolean.parseBoolean(properties.getProperty("generator.service_impl"));
                if (serviceImpl) {
                    String path = properties.getProperty("generator.service_impl.path");
                    getLog().info(path);
                    getLog().info(entity + "service_impl 生成");
                    ServiceImpl.execute(getLog(), path,entity,properties);
                }

                Boolean controller = Boolean.parseBoolean(properties.getProperty("generator.controller"));
                if (controller) {
                    String path = properties.getProperty("generator.controller.path");
                    getLog().info(path);
                    getLog().info(entity + "controller 生成");
                    Controller.execute(getLog(), path,entity);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
